<?php

namespace App\Service\ServiceTrait;

use App\Cache\RedisKey;
use App\Cache\UserScoreCache;
use App\Constants\ErrorCode;
use App\Constants\RecommendedGradeCode;
use App\Exception\ApiException;
use App\Model\Setting\SettingAppHidden;
use App\Model\UserProfile;
use App\Model\UserScore;
use App\Model\UserTypeRule;

trait UserListTrait
{
    use GetUserInfoTrait;

    /**
     * @return array
     */
    protected function getRecommendScoreUserId(): array
    {
        $userInfo = $this->getUserInfo($this->userId);

        if (!in_array($userInfo->gender,[0,1])){
            throw new ApiException('该用户还未完善资料',ErrorCode::LOGIN_TOKEN_EXP);
        }

        $typeInfo = UserScore::getTypeIdByUserId($this->userId);
        $typeId = $typeInfo->type_id;
        if (empty($typeId)){
            $typeId = $userInfo->gender == 0 ? RecommendedGradeCode::FEMALE_DEFAULT_GRADE : RecommendedGradeCode::MALE_DEFAULT_GRADE;
        }

        $recommendType = UserTypeRule::getRecommendType($userInfo->gender,$typeId);
        if (empty($recommendType) || empty($recommendType->recommend_type)){
            $recommendType->recommend_type = $userInfo->gender == 0 ? RecommendedGradeCode::MALE_DEFAULT_GRADE : RecommendedGradeCode::FEMALE_DEFAULT_GRADE;
        }

        switch ($userInfo->gender){
            case 0:
                $recommendTypeCode = RecommendedGradeCode::MALE_GRADE_ARRAY[(int)$recommendType->recommend_type];
                break;
            case 1:
                $recommendTypeCode = RecommendedGradeCode::FEMALE_GRADE_ARRAY[(int)$recommendType->recommend_type];
                break;
        }

        if (empty($recommendTypeCode)){
            throw new ApiException('用户不存在');
        }

        //获取推荐逻辑
        $recommendTypeUser = UserScoreCache::getGradeUser(RedisKey::todayUserGradeList($recommendTypeCode));

        //屏蔽用户
        $shield = SettingAppHidden::getExcludeUsers();

        //去掉屏蔽的用户
        $recommendTypeUserArr = array_diff($recommendTypeUser,$shield,[env('KEFU_UID',7),env('KEFU_UID2',8)]);

        //在线的用户
        switch ($userInfo->gender){
            case 0:
                $onlineUser = $this->getOnlineMaleUser();
                break;
            case 1:
                $onlineUser = $this->getOnlineFemaleUser();
                break;
        }

        if (empty($onlineUser)){
            $onlineUser = [];
        }

        //在线的推荐用户 取交集
        $onlineRecommendUser = array_intersect($recommendTypeUserArr,$onlineUser);

        //不在线的推荐用户 取差集
        $noOnlineUser = array_diff($recommendTypeUserArr,$onlineRecommendUser);

        //合并数组 在线的在前 不在线的在后
        return array_merge($onlineRecommendUser,$noOnlineUser);
    }

    /**
     * 获取在线的用户
     * @return array
     */
    protected function getOnlineFemaleUser(): array
    {
        return UserProfile::where([
            ['online','=',1],
            ['gender','=',0]
        ])->pluck('user_id')->toArray();
    }

    /**
     * 获取在线的男生
     * @return array
     */
    protected function getOnlineMaleUser(): array
    {
        $time = date('Y-m-d H:i:s', strtotime('now') - 1800);

        return UserProfile::where([
            ['gender','=',1],
            ['updated_at','>=',$time]
        ])->pluck('user_id')->toArray();
    }
}